<%@ page language="java" pageEncoding="utf-8" contentType="text/html; charset=utf-8"%>
<%@ include file="../include/taglib.jsp" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  	<%@ include file="../include/jsCss.jsp" %>
  	<title>修改密码</title> 
  	<script language="javascript" src="js/jquery.js"></script>
  	<script language="javascript" src="js/jquery.form.js"></script>
  </head>
  
  <body>
  	<div style="text-align:center;">
	  	<div style="border-style: solid;width:500px; border-width: 1px; border-color: red;">
	  		<h1>修改密码</h1>
	       <form action="">
	       		<table>
	       			<tr>
	       				<td>原密码：</td>
	       				<td>
	       					<input type="password" name="userPwd" id="userPwd" size="19" onblur="validatePwd();"/>
	       					<span id="spanValidatePwd"></span>
	       					<input type="hidden" name="userId" value="${user.userId}" id="userId"/>
	       				</td>
	       			</tr>
	       			<tr>
	       				<td>新密码：</td>
	       				<td>
	       					<input type="password" name="newUserPwd" id="newUserPwd" size="19" onblur="newPasswordValidate();"/>
	       					<span id="newPassword"></span>
	       				</td>
	       			</tr>
	       			<tr>
	       				<td>确认密码：</td>
	       				<td>
	       					<input type="password" name="newUserPwd2" id="newUserPwd2" size="19"/>
	       					<span id="newPasswordAffirm"></span>
	       				</td>
	       			</tr>
	       			<tr>
	       				<td colspan="2" style="text-align:left;"> 
	       					<input type="button" value="确定"  onclick="updatePwd();" style="width: 100px;height:30px;"/>
	       				</td>
	       			</tr>
	       		</table>
	       </form>
	      </div>
      </div>
    <script type="text/javascript"> 
   		function updatePwd() {
   			var userId = $("#userId").val();
			var userPwd = $("#userPwd").val();
   			var newUserPwd = $("#newUserPwd").val();
   			
   			var newUserPwd2 = $("#newUserPwd2").val();
   			
		if (userPwd == "") {
			$("#spanValidatePwd").text("请输入原始密码！");
   			$("#spanValidatePwd").css('color','red');
			document.getElementById("userPwd").focus();
		}
		else if (newUserPwd == "") {
			$("#newPassword").text("请输入新密码！");
			$("#newPassword").css('color','red');
			document.getElementById("newUserPwd").focus();
		}else if (newUserPwd.length < 6) {
			$("#newPassword").text("新密码不能小于六位！");
			$("#newPassword").css('color','red');
			document.getElementById("newUserPwd").focus();
		} else if (newUserPwd2 == "") {
			$("#newPasswordAffirm").text("请输入确认密码！");
			$("#newPasswordAffirm").css('color','red');
			document.getElementById("newUserPwd2").focus();
		} else if (newUserPwd2.length < 6) {
			$("#newPasswordAffirm").text("确认密码密码不能小于六位！");
			$("#newPasswordAffirm").css('color','red');
			document.getElementById("newUserPwd2").focus();
		} else if (newUserPwd2 != newUserPwd) { 
			$("#newPasswordAffirm").text("确认密码与新密码不一样!");
			$("#newPasswordAffirm").css('color','red');
			document.getElementById("newUserPwd2").focus();
		} else { 
			$.ajax( {
				type : "post",
				url : "login!savePwdUpdate.html",
				data : { 
					userPwd : userPwd,
					newUserPwd : newUserPwd,
					userId : userId
				},
				success : function(result) { 
					if (result == 0) {
						alert("密码修改成功,需要你重新登录!");
						//parent.document.reload();
						parent.document.location.href="${getWebUrl}/login.jsp";
					} else if (result == -1) {
						alert("原始密码输入错误！");
						document.getElementById("userPwd").focus();
					} else if (result == -2) {
						alert("你还未登录，请先登录！");
					}
				},
				error : function(result) {
					alert("数据读取失败！");
				}
			});
		}
	}
   		
   	function validatePwd(){
   		
   		var userPwd = $("#userPwd").val();
   		var userId = $("#userId").val();
   		//判断是否为 原来的密码
   		if(userPwd==""){
   			$("#spanValidatePwd").text("请输入原始密码！");
   			$("#spanValidatePwd").css('color','red');
   		}else{
   			$.ajax({
	   			type:"post",
	   			url:"login!selectPwd.html",
	   			data:{userPwd :userPwd,userId:userId},
	   			success : function(result){
	   				if(1 == result){
	   					$("#spanValidatePwd").text("你输入的密码不对，请重新输入!");
	   					$("#spanValidatePwd").css('color','red');
	   					return;
	   				}else{
	   					$("#spanValidatePwd").hide();
	   				}
	   					
	   			},
	   			error : function(result){
	   				alert("读取数据失败!");
	   			}
   			});
   		}
   	}
   	
   	function newPasswordValidate(){
   		var newUserPwd = $("#newUserPwd").val();
   		if(newUserPwd.length < 6){
   			$("#newPassword").text("请输入至少六位数字的密码");
   			$("#newPassword").css('color','red');
   		}else{
   			$("#newPassword").hide();
   		}
   	}
   	
    </script>
  </body>
</html>
